package com.spark.lola.controller.purchaser;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.entity.sup.SupplierInfo;
import com.spark.lola.feign.service.PortalServiceOpenApi;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.request.SupplierInfoRequest;
import com.spark.lola.service.sup.ISupplierInfoService;
import com.spark.lola.support.api.entity.EnterpriseTokenUser;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.common.QueryCondition;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.wms.configuration.WmsProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dengyejun
 * @since 2020-04-24
 */
@RestController
@RequestMapping("/api/pur/supplier-info")
@Api(value = "/api/pur/supplier-info", tags = "供应商信息接口")
public class SupplierInfoController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SupplierInfoController.class);
    @Autowired
    private SupplierInfoMapper currentMapper;
    @Autowired
    private ISupplierInfoService currentService;
    @Autowired
    private PortalServiceOpenApi portalServiceOpenApi;
    @Autowired
    private WmsProperties wmsProperties;

    /**
     * 根据业务获取供应商 0-所有
     *
     * @param salesmanId
     * @return
     */
    @RequestMapping(value = "/map/{salesmanId}", method = RequestMethod.POST)
    public ResponseBean map(@PathVariable(value = "salesmanId") Long salesmanId,
                            @RequestParam("key") String key, @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        if (wmsProperties.getPurchase().getDutyId().get(1).equals(tokenUser.getShiroUser().getDutyId())) {
            salesmanId = tokenUser.getShiroUser().getId();
        }
        return ResponseBean.success(currentService.selectMap(salesmanId, key));
    }

    @ApiOperation(value = "分页查询供应商信息")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findBy(@RequestBody QueryParam<SupplierInfo> queryBean,
                               @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        QueryCondition queryCondition = new QueryCondition();
        queryCondition.setKey("o.salesmanId");
        queryCondition.setOper("=");
        queryCondition.setValue(tokenUser.getShiroUser().getId());

        QueryCondition queryCondition1 = new QueryCondition();
        queryCondition1.setKey("o.used");
        queryCondition1.setOper("=");
        queryCondition1.setValue(CommonConstant.Y);

        queryBean.setQueryList(new ArrayList<>());
        queryBean.getQueryList().add(queryCondition);
        queryBean.getQueryList().add(queryCondition1);
        queryBean.setSortFields("o.createTime_d");
        PageParam<SupplierInfo> pageParam = queryBean.toPageParam();
        IPage<SupplierInfo> page = currentMapper.page(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @ApiOperation(value = "查询供应商信息详情")
    @ApiImplicitParam(name = "id", value = "供应商id", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.POST)
    public ResponseBean findInfo(@PathVariable Long id,
                                 @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        return ResponseBean.success(currentService.getById(id));
    }

    @ApiOperation(value = "保存供应商信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody SupplierInfoRequest form, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        if (currentMapper.checkName(form.getName(), form.getId()) != 0) {
            return ResponseBean.error("名称已存在");
        }
        SupplierInfo model = new SupplierInfo();
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = currentService.getById(form.getId());
        }
        BeanUtils.copyProperties(form, model);
        model.setSalesmanId(tokenUser.getShiroUser().getId());
        currentService.saveOrUpdate(model);
        if (currentMapper.existNo(model.getId(), model.getNo()) != 0) {
            throw new Exception("编码已存在");
        }
        return ResponseBean.success(model);
    }

}
